package cn.com.vrv.service;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import cn.com.vrv.entity.QueryObj;
import cn.com.vrv.entity.RecognitionModel;
import cn.com.vrv.entity.RecognitionTask;
import cn.com.vrv.mapper.RecognitionModelMapper;
import cn.com.vrv.util.StringUtils;

/**
 * <一句话功能简述>
 * 
 * @author 赵炎
 * @version [V1.00, 2017年11月21日]
 * @see [相关类/方法]
 * @since V1.00
 */
@Service
public class RecognitionModelService
{
    @Autowired
    private RecognitionModelMapper recognitionModelMapper;
    
    public List<RecognitionModel> findAll()
    {
    	return recognitionModelMapper.findAll();
    }
    
    
    public Page<RecognitionModel> findBookCriteria(Integer page, Integer size, final QueryObj obj) {  
        Pageable pageable = new PageRequest(page, size, Sort.Direction.ASC, "createTime");  
        Page<RecognitionModel> bookPage = this.recognitionModelMapper.findAll(new Specification<RecognitionModel>(){  
        	 @Override
             public javax.persistence.criteria.Predicate toPredicate(Root<RecognitionModel> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                 Path<String> namePath = root.get("modelName");
                 Path<String> taskType = root.get("modelType");
                 /**
                  * 连接查询条件, 不定参数，可以连接0..N个查询条件
                  */
                 if(StringUtils.isNotBlank(obj.getFileName())&&StringUtils.isNotBlank(obj.getTaskType()))
                 {
                	 query.where(cb.like(namePath, "%" + obj.getFileName() + "%"),cb.like(taskType, "%" + obj.getTaskType() + "%")); //这里可以设置任意条查询条件
                	 return null;
                 }
                 if(StringUtils.isNotBlank(obj.getTaskType()))
                 {
                	 query.where(cb.like(taskType, "%" + obj.getTaskType() + "%")); //这里可以设置任意条查询条件
                	 return null;
                 }
                 if(StringUtils.isNotBlank(obj.getFileName()))
                 {
                	 query.where(cb.like(namePath, "%" + obj.getFileName() + "%")); //这里可以设置任意条查询条件
                	 return null;
                 }
                 if(StringUtils.isBlank(obj.getFileName())&&StringUtils.isBlank(obj.getTaskType()))
                 {
                	 query.where(); //这里可以设置任意条查询条件
                	 return null;
                 }
                 
                 return null;
             }  
        },pageable);  
        return bookPage;  
    }
    
    public void save(RecognitionModel model)
    {
        
        this.recognitionModelMapper.save(model);
    }
    
    public RecognitionModel findByModelId(String modelId)
    {
        
        return this.recognitionModelMapper.findByModelId(new Long(modelId));
    }
    
    public void deleteByModelId(String modelId)
    {
    	 this.recognitionModelMapper.deleteByModelId(new Long(modelId));
    }
    
    public void updateByModelId(RecognitionModel recognitionModel)
    {
    	this.recognitionModelMapper.updateByModelId(recognitionModel.getModelName(), recognitionModel.getModelPath(), recognitionModel.getModelType(), recognitionModel.getModelDescription(), recognitionModel.getModelId());

    }
}
